package com.example.wei.mvp.base;

import android.app.Application;
import android.os.Build;
import android.os.Handler;
import android.text.TextUtils;

import com.example.wei.mvp.component.AppComponent;
import com.example.wei.mvp.component.DaggerAppComponent;
import com.example.wei.mvp.module.ApiModule;
import com.example.wei.mvp.module.AppModule;
import com.example.wei.mvp.util.AppUtils;
import com.example.wei.mvp.util.FileUtil;
import com.tencent.bugly.crashreport.CrashReport;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

/**
 * Created by Administrator on 2017/1/16.
 */

public class App extends Application {

    private static App instance;
    private AppComponent appComponent;
    public static String appVersion;
    public static int mMainThreadId;
    public static Handler mHandler;
    @Override
    public void onCreate() {
        super.onCreate();
        mHandler=new Handler();
         instance = this;
        FileUtil.initFileCache(this);
        initBugly();
        initComponent();
        initApp();

    }
    public static String initEngineManager(){
        String useragent = "APP-Android-" + AppUtils.getVerName(getInstance()) + "-" + Build.VERSION.SDK_INT + "-" + Build.BRAND + "-" + Build.MODEL;
        return  useragent;
    }

    public static App getInstance() {
        return instance;
    }

    private void initComponent() {
        appComponent = DaggerAppComponent.builder()
                .apiModule(new ApiModule())
                .appModule(new AppModule(this))
                .build();
    }

    public AppComponent getAppComponent() {
        return appComponent;
    }

    private void initApp() {
        try {
            this.appVersion = this.getPackageManager().getPackageInfo(this.getPackageName(), 0).versionName;
        } catch (Exception e) {
            this.appVersion = "1.0.0";
        }

    }


    /**
     * 初始化bugly
     */
    private void initBugly() {
        String packageName = getPackageName();
        // 获取当前进程名
        String processName = getProcessName(android.os.Process.myPid());
        // 设置是否为上报进程
        CrashReport.UserStrategy strategy = new CrashReport.UserStrategy(this);
        strategy.setUploadProcess(processName == null || processName.equals(packageName));
        // 初始化Bugly  建议在测试阶段建议设置成true，发布时设置为false。
        CrashReport.initCrashReport(this, "78563f4847", false, strategy);

    }

    /**
     * 获取进程号对应的进程名
     *
     * @param pid 进程号
     * @return 进程名
     */
    private static String getProcessName(int pid) {
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new FileReader("/proc/" + pid + "/cmdline"));
            String processName = reader.readLine();
            if (!TextUtils.isEmpty(processName)) {
                processName = processName.trim();
            }
            return processName;
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        } finally {
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (IOException exception) {
                exception.printStackTrace();
            }
        }
        return null;
    }

    /**
     * 返回Handler
     * @return
     */
    public static Handler getHandler() {
        return mHandler;
    }

    /**
     * 返回主线程的pid
     * @return
     */
    public static int getMainThreadId() {
        return android.os.Process.myTid();
    }

}
